<?php

	namespace Aspic\Model;

	use Aspic as A;
	
	/**
	 * Provide a Table object for each TableModel and well used methods links 
	 * to it.
	 *
	 * @author Mallo
	 */
	abstract class AbstractTableModel extends AbstractModel {

		/**
		 * @var QueryExecuterInterface Database object that implement QueryExecuterInterface
		 */
		protected static $db;
		
		/**
		 *
		 * @var string The namespace of the database type (Mysql, Sqlite, ..)
		 * Used to create Table object
		 */
		protected $databaseTypeNamespace;
		
		protected $table;
		
		public function __construct($databaseTypeNamespace) {
			$this->setDatabaseTypeNamespace($databaseTypeNamespace);
			
			parent::__construct();
		}
		
		protected function setDatabaseTypeNamespace($databaseTypeNamespace) {
			$this->databaseTypeNamespace = $databaseTypeNamespace;
		}
		
		protected function getDatabaseTypeNamespace() {
			return $this->databaseTypeNamespace;
		}
		
		/**
		 * Have to init the static::$db object
		 */
		abstract protected static function initData();
		
		/**
		 * Create a Table object ($this->table) from given table name.
		 * When using ServiceManager, this method should be called in 
		 * serviceInit() to inject dependencies.
		 * @param string $tableName Name of the table
		 */
		protected function initTable(TableDataModel $dataModel) {
			$this->table = A\Database\AbstractDatabase::factory(
				'Table', 
				$this->getDatabaseTypeNamespace(), 
				$dataModel, 
				static::$db
			);
		}

	}

?>
